const Zan = require('../../../dist/index');
const config = require('../../../config.js');
const util = require('../../../utils/util.js')

let formData = {
  location: "",
  remarks: "",
  userId: "",
  coordinated: "",
  completed: "",
  projectid: ""
}
Page(Object.assign({}, Zan.Field, {
  /**
   * 页面的初始数据
   */
  data: {
    area: ['请选择', '项目1', '项目2'],
    areaValue: [],
    areaIndex: 0,
    user:[],
    userValue:[],
    userIndex:0,
    userIconPath: "",
   duifang:"",
   jiaoyu:"",
   qita:"",
    selectedUsers:[]
  },


  onAreaChange(e) {
    this.setData({
      areaIndex: e.detail.value
    });
  },
  onUserChange(e) {
    var a = [].concat(this.data.selectedUsers,this.data.userValue[e.detail.value]);
    this.setData({
      userIndex: e.detail.value,
      selectedUsers:a
    });

    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(config)
    util.postData(config.selectProjUrl, {
      pageNo: 1,
      pageSize: 50
    }).then(res => {
      console.log(res)
      this.setData({
        area: res.body.data.map(x => x.projectName),
        areaValue: res.body.data.map(x => x.id)
      });

    })


    util.postData(config.selectUserUrl, {
      pageNo: 1,
      pageSize: 50
    }).then(res => {
      console.log(res)
      res.body.data.forEach(x => x.photo = 'http://58.210.195.230:8818/'+x.photo)
      this.setData({
        user: res.body.data.map(x => x.name),
        userValue: res.body.data
      });

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    wx.getUserInfo({
      success: function(res) {
        var userInfo = res.userInfo
        var nickName = userInfo.nickName
        var avatarUrl = userInfo.avatarUrl
        var gender = userInfo.gender //性别 0：未知、1：男、2：女
        var province = userInfo.province
        var city = userInfo.city
        var country = userInfo.country

        that.setData({
          userIconPath: avatarUrl
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  complete: function(e) {
 formData.completed = e.detail.value
  },
  coord: function(e) {
    formData.coordinated = e.detail.value
  },
  remark: function(e) {
    formData.remarks = e.detail.value
  },
  createLog: function() {
    wx.getLocation({
      success: function(res) {
        formData.location = res.longitude + "," + res.latitude;
      }
    })
    formData.projectid = this.data.areaValue[this.data.areaIndex]
    formData.userId = this.data.selectedUsers.map(x => x.id).reduce((x, y) => x + y)

    util.postData(config.createLogUrl, formData).then(res=>{
      wx.uploadFile({
        url: "http://58.210.195.230:8818/jeeplus/a/wx/interface/uploadFile;JSESSIONID=" + wx.getStorageSync("JSESSIONID"),
        filePath: this.data.duifang,
        name: 'file',
        formData: { "module": "projmylog/projMyLog/stacked" }
      })
      wx.uploadFile({
        url: "http://58.210.195.230:8818/jeeplus/a/wx/interface/uploadFile;JSESSIONID=" + wx.getStorageSync("JSESSIONID"),
        filePath: this.data.jiaoyu,
        name: 'file',
        formData: { "module": "projmylog/projMyLog/education" }
      })
      wx.uploadFile({
        url: "http://58.210.195.230:8818/jeeplus/a/wx/interface/uploadFile;JSESSIONID=" + wx.getStorageSync("JSESSIONID"),
        filePath: this.data.qita,
        name: 'file',
        formData: { "module": "projmylog/projMyLog/others" }
      })
      wx.navigateBack({

      })
    })

  },
  chooseDuiFang: function() {
    wx.chooseImage({
      count: 1,
      success: res=> {
        this.setData({
          duifang: res.tempFilePaths[0]
        })
      },
    })
  },
  chooseEdu: function() {
    wx.chooseImage({
      count: 1,
      success: res => {
        this.setData({
          jiaoyu: res.tempFilePaths[0]
        })
      },
    })
  },
  chooseImg: function() {
    wx.chooseImage({
      count: 1,
      success: res => {
        this.setData({
          qita: res.tempFilePaths[0]
        })
      },
    })
  }


}))